using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using SpaceWolf.GameLogic;
using SpaceWolf.Graphics;
using SpaceWolf.GameScreens;
using SpaceWolf.Properties;

namespace SpaceWolf
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Elite : SpaceWolf.BaseGame
    {
        /// <summary>
        /// Game screens stack. We can easily add and remove game screens
        /// and they follow the game logic automatically. Very cool.
        /// </summary>
        private static Stack<IGameScreen> gameScreens = new Stack<IGameScreen>();

        GameManager gameManager = new GameManager();
        public Elite()
        {
            Sounds.Sound.Play("MainTheme");

        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            base.Initialize();
            // TODO: Add your initialization logic here
            InputManager.ResetActionMap();

            gameManager.loadModels();
            
            gameScreens.Push(new CommanderScreen(ref gameManager));
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {

            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here
            if (!gameManager.GameOver)
            {
                #region Mouse On Planet
                if (InputManager.MouseLeftButtonJustPressed)
                {

                    // Commander
                    if (InputManager.MouseInBox(new Rectangle(10, BaseGame.Height - 73, 64, 64)))
                    {
                        Sounds.Sound.Play("ButtonClick");
                        gameScreens.Push(new CommanderScreen(ref gameManager));
                    }
                    // Market
                    if (InputManager.MouseInBox(new Rectangle(75, BaseGame.Height - 73, 64, 64)))
                    {
                        Sounds.Sound.Play("ButtonClick");
                        gameScreens.Push(new MarketScreen(ref gameManager));
                    }
                    // Map
                    if (InputManager.MouseInBox(new Rectangle(140, BaseGame.Height - 73, 64, 64)))
                    {
                        Sounds.Sound.Play("ButtonClick");
                        gameScreens.Push(new GalacticMapScreen(ref gameManager));
                    }
                    //  Armory
                    if (InputManager.MouseInBox(new Rectangle(205, BaseGame.Height - 73, 64, 64)))
                    {
                        Sounds.Sound.Play("ButtonClick");
                        gameScreens.Push(new MilitaryMarketScreen(ref gameManager));
                    }
                    //  Ships
                    if (InputManager.MouseInBox(new Rectangle(270, BaseGame.Height - 73, 64, 64)))
                    {
                        Sounds.Sound.Play("ButtonClick");
                        //gameScreens.Push(new MilitaryMarketScreen(ref gameManager));
                        //gameScreens.Push(new Options());
                    }
                    //  Journal
                    if (InputManager.MouseInBox(new Rectangle(335, BaseGame.Height - 73, 64, 64)))
                    {
                        Sounds.Sound.Play("ButtonClick");
                        gameScreens.Push(new MissionsScreen(ref gameManager));
                    }
                    //  Planet
                    if (InputManager.MouseInBox(new Rectangle(400, BaseGame.Height - 73, 64, 64)))
                    {
                        Sounds.Sound.Play("ButtonClick");
                        gameScreens.Push(new PlanetScreen(ref gameManager));
                    }
                    //  Options
                    if (InputManager.MouseInBox(new Rectangle(530, BaseGame.Height - 73, 64, 64)))
                    {
                        Sounds.Sound.Play("ButtonClick");
                        gameScreens.Push(new Options());
                    }

                }//if (InputManager.MouseLeftButtonJustPressed)
                #endregion

                #region Keyboard on planet
                switch (InputManager.ActionPressed())
                {

                    case Action.PlanetData:
                        gameScreens.Push(new PlanetScreen(ref gameManager));
                        break;
                    case Action.MarketData:
                        gameScreens.Push(new MarketScreen(ref gameManager));
                        break;
                    case Action.MilitaryData:
                        gameScreens.Push(new MilitaryMarketScreen(ref gameManager));
                        break;
                    case Action.CommanderData:
                        gameScreens.Push(new CommanderScreen(ref gameManager));
                        break;
                    case Action.LaunchMyShip:
                            gameScreens.Push(new GameScreen(ref gameManager));
                        break;
                    case Action.ShortGalaxyChart:
                        gameScreens.Push(new GalacticMapScreen(ref gameManager));
                        break;
                }//switch (InputManager.ActionPressed())
                #endregion

                #region In Space
                if (gameManager.InSpace)
                {
                    gameManager.update(gameTime);
                    // try to dock to planet
                    if (InputManager.IsActionPressed(Action.DockingComputer))
                    {
                        if(gameManager.DockShip())
                             gameScreens.Push(new CommanderScreen(ref gameManager));
                    }

                    // Jump to sellected planet
                    if (InputManager.IsActionPressed(Action.JumpToPlanet))
                    {
                        gameManager.JumpToPlanet();
                    }

                    // laser FIRE !!!
                     if(InputManager.MouseLeftButtonPressed)
                         gameManager.LaserFire();

                    if (InputManager.IsActionTriggered(Action.NextTarget))
                    {
                        gameManager.GetNextTarget();
                    }

                    if (InputManager.IsActionTriggered(Action.PreviousTarget))
                    {
                        gameManager.GetPreviousTarget();
                    }

                    if (InputManager.IsActionPressed(Action.ShipSpeedUp))
                    {
                        gameManager.IncreaseFlightSpeed();
                    }

                    if (InputManager.IsActionPressed(Action.ShipSpeedDown))
                    {
                        gameManager.DecreaseFlightSpeed();
                    }
                }
                #endregion

            }// if (!gameManager.GameOver)
            else
            {
                //gameManager.update(gameTime);
            }
            InputManager.Update();

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        //protected override void Draw(GameTime gameTime)
        //{
        //    ////graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
            
        //    //// TODO: Add your drawing code here
            
        //    ////
        //    //switch (iScreenNum)
        //    //{
        //    //    case 0:
        //    //        UI.RenderGameUI();
        //    //        gameManager.ShowMarketPrices();
        //    //        break;
        //    //    case 1:
        //    //        UI.RenderGameUI();
        //    //        gameManager.ShowPlanetDesc();
        //    //        break;
        //    //    case 2:
        //    //        UI.RenderGameUI();
        //    //        gameManager.ShowCommanderStatus();
        //    //        break;
        //    //    case 3:
        //    //        UI.RenderSpaceUI();
        //    //        if(gameManager.IsShipDocked())
        //    //            gameManager.UnDockShip();
        //    //        gameManager.Flight();
                    
        //    //        break;
        //    //    case 4:
        //    //        UI.RenderGameUI();
        //    //        gameManager.DisplayShortGalaxyChart();
        //    //        break;
        //    //}
            
        //    //base.Draw(gameTime);
        //}

        protected override void Render()
        {
            // No more game screens?
            if (gameScreens.Count == 0)
            {
                // Before quiting, stop music and play crash sound :)
                // Then quit
                Exit();
                return;
            } // if (gameScreens.Count)

            // Handle current screen
			if (gameScreens.Peek().Render())
			{
				// If this was the options screen and the resolution has changed,
				// restart the game!
                if (gameScreens.Peek().GetType() == typeof(Options) &&
                    (BaseGame.Width != GameSettings.Default.ResolutionWidth ||
                    BaseGame.Height != GameSettings.Default.ResolutionHeight ||
                    BaseGame.IsFullScreen != GameSettings.Default.Fullscreen))
                {
                    // Restart if resolution was changed!
                    Program.RestartGameAfterOptionsChange = true;
                    this.Exit();
                } // if

				// Play sound for screen back

				gameScreens.Pop();
			} // if (gameScreens.Peek)
        }

        #region Add game screen
        /// <summary>
        /// Add game screen
        /// </summary>
        /// <param name="gameScreen">Game screen</param>
        public static void AddGameScreen(IGameScreen gameScreen)
        {
            // Play sound for screen click
            //Sound.Play(Sound.Sounds.ScreenClick);

            // Add the game screen
            gameScreens.Push(gameScreen);
        } // AddGameScreen(gameScreen)
        #endregion
    }
}
